#!/bin/sh
if [ $# -lt 1 ] 
then
    echo "Usage: ./get_reverse_link.sh srcFile";
    echo "File structure:"
    echo "id1""\t""id2,id3,id4"
    exit 1;
else
    src_file=$1
fi

awk '{
    split($2, relates, ",");
    for (id in relates) {
        if (length(relates[id]) < 1) continue;
        if (relates[id] in reverse_links) {
            reverse_links[relates[id]] = reverse_links[relates[id]]","$1;
        }
        else {
            reverse_links[relates[id]] = $1;
        }
    }
}
END {
    for (qId in reverse_links) {
        print qId, reverse_links[qId];
    }
}' $src_file
